import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException,IOException {
        String username = request.getParameter("username");
        String psw = request.getParameter("psw");
        if ("xuchao".equals(username)&&"123".equals(psw)){
        response.sendRedirect(request.getContextPath()+"/index.html");
        }else {
            response.setContentType("text/html;charset=UTF-8");
            response.getWriter().write("登陆失败请重新登陆,2秒后自动跳转...");
            response.setHeader("Refresh","2;URL=login.html");
        }

        //cookie用于记录服务器给浏览器的信息
        Cookie cookie = new Cookie("xx","xx");
        response.addCookie(cookie);
        Cookie[] cookies = request.getCookies();
        //session用于获取传入域空间的信息
        String name1 = request.getParameter("name");
        HttpSession session = request.getSession();
        session.setAttribute("name1",name1);
        String name = (String) session.getAttribute("name1");
        for (Cookie cookie1 : cookies) {

        }
    }
}
